package echat.future.cn.echat.my.view;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import echat.future.cn.echat.R;
import echat.future.cn.echat.databinding.ActivityMyFanseBinding;
import echat.future.cn.echat.home.ChatActivity;
import echat.future.cn.echat.my.adapter.FanseListAdapter;
import echat.future.cn.echat.my.api.UserApi;
import echat.future.cn.echat.my.bean.FansBean;
import echat.future.cn.echat.my.utils.HttpUtilsRequst;
import echat.future.cn.echat.my.utils.UserHelper;
import echat.future.cn.echat.my.weight.Navagation.AdapterNavagation;
import echat.future.cn.echat.my.weight.Navagation.IBaseNavagation;
import lumingweihua.future.cn.baselibgxh.dialog.YesOrNoDialog;
import lumingweihua.future.cn.baselibgxh.dialog.YesOrNoDialogEntity;

public class MyFanseActivity extends BaseActivity<ActivityMyFanseBinding>{

    FanseListAdapter fanseListAdapter;
    List<FansBean> mDatas = new ArrayList<>();

    @Override
    public int layoutId() {
        return R.layout.activity_my_fanse;
    }

    @Override
    public IBaseNavagation navagation() {
        return new AdapterNavagation(this).setTitle("我的粉丝", 16, R.color.white).setBack(R.drawable.btn_back).setNavagationBackgroudColor(R.color.color_161823);
    }

    @Override
    public void initView() {
        fanseListAdapter = new FanseListAdapter(this.mDatas);
        bind.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        bind.recyclerView.setAdapter(fanseListAdapter);
        fanseListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if(view.getId() == R.id.add){
                    new YesOrNoDialog(MyFanseActivity.this, new YesOrNoDialogEntity("确认将他移入黑名单?", "取消", "确认"), new YesOrNoDialog.OnYesOrNoDialogClickListener() {
                                @Override
                                public void onYesOrNoDialogClick(YesOrNoDialog.YesOrNoType yesOrNoType) {
                                    switch (yesOrNoType) {
                                        case BtnOk:
                                            addBlackList(mDatas.get(position).getFans_id());
                                            break;

                                        default:
                                            break;
                                    }
                                }
                            }).show();
                }
            }
        });

        fanseListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                new YesOrNoDialog(MyFanseActivity.this, new YesOrNoDialogEntity("是否和他私信?", "取消", "确认"), new YesOrNoDialog.OnYesOrNoDialogClickListener() {
                            @Override
                            public void onYesOrNoDialogClick(YesOrNoDialog.YesOrNoType yesOrNoType) {
                                switch (yesOrNoType) {
                                    case BtnOk:
                                        startActivity(new Intent(MyFanseActivity.this, ChatActivity.class)
                                            .putExtra("id", mDatas.get(position).getMobile())
                                            .putExtra("avatar",  mDatas.get(position).getAvatar())
                                            .putExtra("username",  mDatas.get(position).getUsername()));
                                        break;

                                    default:
                                        break;
                                }
                            }
                        }).show();
            }
        });

        loadMore();
    }

    private void loadMore(){
        new HttpUtilsRequst<List<FansBean>>(this,HttpUtilsRequst.retrofitClient.create(UserApi.class).getMyFans(UserHelper.getToken()))
                .setmOnRequstSuccessListener((HttpUtilsRequst.OnRequstSuccessListener<List<FansBean>>) o -> {
                    mDatas.clear();
                    mDatas.addAll(o);
                    fanseListAdapter.notifyDataSetChanged();
                })
                .start();
    }

    /**
     * 加入黑名单
     */
    private void addBlackList(int id){
        new HttpUtilsRequst<String>(this,HttpUtilsRequst.retrofitClient.create(UserApi.class).addBlackList(UserHelper.getToken(),id))
                .setmOnRequstSuccessListener(o -> {
                    toast("加入黑名单成功");
                    loadMore();
                })
                .start();
    }

}
